package cn.hebutrun.oulabot.common.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.experimental.UtilityClass;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * @author zhaolong
 * @since 2022-09-07
 */
@UtilityClass
public class TimeUtil {

  public Instant toInstant(LocalDateTime localDateTime) {
    return localDateTime.toInstant(ZoneOffset.of("+8"));
  }

  public long toEpochMilli(LocalDateTime localDateTime) {
    return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  }

  public String toEpochMilliStr(LocalDateTime localDateTime) {
    return Convert.toStr(toEpochMilli(localDateTime));
  }

  public DateTime toDateTime(LocalDateTime localDateTime) {
    return DateTime.of(toEpochMilli(localDateTime));
  }

  public Date toDate(LocalDateTime localDateTime) {
    return Date.from(toInstant(localDateTime));
  }

  public static String dayOfWeek(Date date) {
    return switch (DateUtil.dayOfWeek(date)) {
      case 1 -> "星期天";
      case 2 -> "星期一";
      case 3 -> "星期二";
      case 4 -> "星期三";
      case 5 -> "星期四";
      case 6 -> "星期五";
      case 7 -> "星期六";
      default -> "";
    };
  }

  public static Date getNextEight() {
    Date now = new Date();
    DateTime todayEight = DateUtil.beginOfDay(now).offset(DateField.HOUR_OF_DAY, 8);
    if (todayEight.isAfterOrEquals(now)) {
      return todayEight;
    } else {
      return todayEight.offset(DateField.DAY_OF_YEAR,1);
    }
  }
}
